using System;
using System.Collections.Generic;
using UnityEngine;

public class CityBuildingElementHandler : MonoBehaviour
{
	public CityBuildingVisualHandler Visual;

	public GameObject LockedDock;

	public GameObject SelectionVisual;

	public CityBuildingUnlockTooltip UnlockToolTip;

	public CityBuildingCollectTooltip CollectToolTip;

	public int BuildId;

	public ElementsDataHolder ElementsDataHolder;

	public CityBuildingElementData Data;

	public List<GameObject> LockedBricks;

	public GameObject UnlockFX;

	public GameObject X2FX;

	private void Awake()
	{
		if ((bool)ElementsDataHolder)
		{
			Data = ElementsDataHolder.ElementData(BuildId);
		}
		for (int i = 0; i < LockedBricks.Count; i++)
		{
			bool active = i == Data.Level % LockedBricks.Count;
			LockedBricks[i].SetActive(active);
		}
		SelectionVisual.SetActive(value: false);
		CityBuildingManager instance = CityBuildingManager.Instance;
		instance.OnElementSelected = (Action<CityBuildingElementHandler>)Delegate.Combine(instance.OnElementSelected, new Action<CityBuildingElementHandler>(OnElementSelected));
		CityBuildingManager instance2 = CityBuildingManager.Instance;
		instance2.OnRVX2 = (Action<bool>)Delegate.Combine(instance2.OnRVX2, new Action<bool>(OnRvx2));
		CityBuildingManager instance3 = CityBuildingManager.Instance;
		instance3.OnCollectGems = (Action)Delegate.Combine(instance3.OnCollectGems, new Action(Refresh));
		Data.Initialize();
	}

	private void Start()
	{
		CollectToolTip.Setup(Data.GiveGems ? CityBuildingManager.Instance.GemSprite : CityBuildingManager.Instance.CashSprite);
		Refresh();
	}

	private void OnRvx2(bool enable)
	{
		Visual.OnRvx2(enable);
		X2FX.SetActive(enable && Data.Unlocked);
	}

	private void OnDestroy()
	{
		if ((bool)CityBuildingManager.Instance)
		{
			CityBuildingManager instance = CityBuildingManager.Instance;
			instance.OnElementSelected = (Action<CityBuildingElementHandler>)Delegate.Remove(instance.OnElementSelected, new Action<CityBuildingElementHandler>(OnElementSelected));
			CityBuildingManager instance2 = CityBuildingManager.Instance;
			instance2.OnRVX2 = (Action<bool>)Delegate.Remove(instance2.OnRVX2, new Action<bool>(OnRvx2));
			CityBuildingManager instance3 = CityBuildingManager.Instance;
			instance3.OnCollectGems = (Action)Delegate.Remove(instance3.OnCollectGems, new Action(Refresh));
		}
	}

	private void OnElementSelected(CityBuildingElementHandler elem)
	{
		SelectionVisual.SetActive(elem == this);
	}

	private void Update()
	{
		if (Data.Unlocked && Data.MoneyToCollect > 0)
		{
			CollectToolTip.SetMoney(Data.MoneyToCollect);
		}
		else
		{
			CollectToolTip.SetMoney(0);
		}
	}

	public void Refresh()
	{
		if (Data.Unlocked)
		{
			UnlockToolTip.Hide();
			Visual.SetLevel(Data.Level);
			Visual.gameObject.SetActive(value: true);
		}
		else
		{
			Visual.gameObject.SetActive(value: false);
			if (Data.UnlockLevel <= CityBuildingManager.Instance.CurrentLevel + 1 && (CityBuildingFirstTimePopup.HasBeenShown || Data.UnlockLevel == CityBuildingManager.Instance.CurrentLevel))
			{
				UnlockToolTip.Setup(Data.UnlockLevel, Data.UnlockPrice, CityBuildingManager.Instance.CurrentLevel >= Data.UnlockLevel, CityBuildingManager.Instance.Gems >= Data.UnlockPrice);
			}
			else
			{
				UnlockToolTip.Hide();
			}
		}
		LockedDock.SetActive(!Data.Unlocked);
	}

	[ExposeMethodInEditor(false, false)]
	public void Select()
	{
		if (Data.Unlocked && Data.MoneyToCollect > 0)
		{
			DoCollect();
		}
		if (Data.UnlockLevel <= CityBuildingManager.Instance.CurrentLevel || Data.Unlocked)
		{
			CityBuildingManager.Instance.Select(this);
			CityBuildingManager.Instance.DoLightHaptic();
		}
	}

	[ExposeMethodInEditor(false, false)]
	public void DoReset()
	{
		Data.LastCollect = DateTime.Now;
		Data.Level = 0;
		Data.Unlocked = false;
		Refresh();
	}

	[ExposeMethodInEditor(false, false)]
	public void Unlock()
	{
		Data.Unlocked = true;
		Data.LastCollect = DateTime.Now.AddMinutes(-5.0);
		Refresh();
		UnlockFX.SetActive(value: false);
		UnlockFX.SetActive(value: true);
		CityBuildingManager.Instance.DoHaptic();
		CityBuildingManager.Instance.UIAudio.PlaySound(UISoundType.BuildingUpgrade);
	}

	[ExposeMethodInEditor(false, false)]
	public void LevelUp()
	{
		Data.Level++;
		if ((DateTime.Now - Data.LastCollect).TotalMinutes < 5.0)
		{
			Data.LastCollect = DateTime.Now.AddMinutes(-5.0);
		}
		UnlockFX.SetActive(value: false);
		UnlockFX.SetActive(value: true);
		Refresh();
		CityBuildingManager.Instance.DoHaptic();
		CityBuildingManager.Instance.UIAudio.PlaySound(UISoundType.BuildingUpgrade);
	}

	[ExposeMethodInEditor(false, false)]
	public void DoCollect()
	{
		RectTransform destination;
		int steps;
		if (Data.GiveGems)
		{
			destination = OldGenericRewardAnimator.Instance.references.GemDefaultDestination;
			CityBuildingManager.Instance.Gems += Data.MoneyToCollect;
			steps = Mathf.Min(30, Data.MoneyToCollect);
		}
		else
		{
			destination = OldGenericRewardAnimator.Instance.references.CashDefaultDestination;
			CityBuildingManager.Instance.Money += Data.MoneyToCollect;
			steps = Mathf.Min(40, Data.MoneyToCollect / 10);
		}
		OldGenericRewardAnimator.Instance.SetDefaultOriginPosition(Camera.main.WorldToScreenPoint(CollectToolTip.transform.position) - new Vector3(Screen.width, Screen.height) / 2f);
		OldGenericRewardAnimator.Instance.AnimateCurrency(Data.MoneyToCollect, steps, null, destination, new Vector2(100f, 100f), useText: false, useCurrency: true, null, null, 0f, null, 200f, Data.GiveGems ? CityBuildingManager.Instance.GemSprite : CityBuildingManager.Instance.CashSprite);
		Data.LastCollect = DateTime.Now;
		Refresh();
	}
}
